package com.android.hidephotosplus;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.android.hidephotosplus.adapter.MediaAdapter;
import com.android.hidephotosplus.db.DbProvider;
import com.android.hidephotosplus.interfaces.MInterface.onTask;
import com.android.hidephotosplus.model.ModelAlbum;
import com.android.hidephotosplus.model.ModelMedia;
import com.android.hidephotosplus.util.Config;
import com.android.hidephotosplus.util.PasswordManager;
import com.android.hidephotosplus.util.Util;

public class ActivityAlbumViewForResult extends SherlockActivity implements
		OnScrollListener, OnItemClickListener {

	private GridView mGridView;

	private MediaAdapter mMediaAdapter;

	private ArrayList<ModelMedia> arrMedias;

	private long idAlbum = -1;

	private long idAlbumHidden = -1;

	private String nameAlbumHidden = null;

	private ActionMode mActionMode;

	private boolean isStartActionMode = false;

	private boolean isCheckAll = false;

	private ArrayList<ModelMedia> arrMediasSelected = new ArrayList<ModelMedia>();

	private ArrayList<ModelMedia> arrMediasSelectedTmp = new ArrayList<ModelMedia>();

	private DbProvider mDbProvider;

	private ModelAlbum mAlbum;

	private String nameAlbum;

	public static boolean IS_START_PASSWORD = false;

	public PasswordManager mPasswordManager;

	private ActionBar mActionBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_media_view);
		mDbProvider = DbProvider.getinstance(getApplicationContext());
		mGridView = (GridView) findViewById(R.id.gridMedia);
		mGridView.setOnScrollListener(this);
		mGridView.setOnItemClickListener(this);
		
		mActionBar = getSupportActionBar();
		mActionBar.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.bg_actionbar));

		mPasswordManager = new PasswordManager(this);
		
		Intent intent = getIntent();
		if (intent.getExtras() != null) {
			mAlbum = intent.getExtras().getParcelable(Config.KEY_ALBUM_STOCK);
			if (mAlbum != null) {
				idAlbum = mAlbum.getIdAlbum();
				nameAlbum = mAlbum.getNameAlbum();
				idAlbumHidden = intent.getExtras().getLong(
						Config.KEY_ID_ALBUM_HIDDEN);
				nameAlbumHidden = intent.getExtras().getString(
						Config.KEY_NAME_ALBUM_HIDDEN);
				if (idAlbum != -1) {
					arrMedias = mDbProvider.getAllMedias(idAlbum);
					mMediaAdapter = new MediaAdapter(getApplicationContext(),
							arrMedias, true);
					mGridView.setAdapter(mMediaAdapter);
					mActionMode = startActionMode(new AnActionMode());
				}
			}

		}

		int doneButtonId = Resources.getSystem().getIdentifier(
				"action_mode_close_button", "id", "android");
		View doneButton = findViewById(doneButtonId);
		doneButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ActivityAlbumViewForResult.this,
						ActivityGalleryForResult.class);
				intent.putParcelableArrayListExtra(
						Config.KEY_REQUEST_ARR_MEDIA_TO_HIDE,
						arrMediasSelectedTmp);
				if (arrMediasSelectedTmp != null
						&& arrMediasSelectedTmp.size() > 0)
					setResult(RESULT_OK, intent);
				else
					setResult(RESULT_CANCELED);
				finish();
				arrMediasSelectedTmp.clear();
			}
		});

	}

	@Override
	public void onPause() {
		super.onPause();
		IS_START_PASSWORD = true;
	}

	@Override
	public void onResume() {
		super.onResume();
		if (IS_START_PASSWORD) {
			mPasswordManager.checkSetupOrUnlockPass();
		}
	}

	private class AnActionMode implements ActionMode.Callback {
		@Override
		public boolean onCreateActionMode(ActionMode mode, Menu menu) {
			MenuInflater inflater = getSupportMenuInflater();
			inflater.inflate(R.menu.actionmode_album_view_stock, menu);
			isStartActionMode = true;
			return true;
		}

		@Override
		public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
			return false;
		}

		@Override
		public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

			switch (item.getItemId()) {
			case R.id.menu_check_all:
				if (!isCheckAll) {
					setCheckUncheckAll(true);
					isCheckAll = true;
				} else {
					setCheckUncheckAll(false);
					isCheckAll = false;
				}
				break;
			case R.id.menu_hide:
				new Util.HiddenMediaInAlbumView(
						ActivityAlbumViewForResult.this, idAlbumHidden,
						getString(R.string.default_album), arrMediasSelected,
						true, new onTask() {
							@Override
							public void onTaskComplete(Object object) {
								arrMediasSelectedTmp.addAll(arrMediasSelected);
								ActivityAlbumViewForResult.this.arrMedias
										.removeAll(arrMediasSelected);
								mMediaAdapter.notifyDataSetChanged();
								arrMediasSelected.clear();
								setTitleActionMode();
							}
						}).execute();
				break;
			default:
				break;
			}

			return true;
		}

		@Override
		public void onDestroyActionMode(ActionMode mode) {
			Intent intent = new Intent(ActivityAlbumViewForResult.this,
					ActivityGalleryForResult.class);
			intent.putParcelableArrayListExtra(
					Config.KEY_REQUEST_ARR_MEDIA_TO_HIDE, arrMediasSelectedTmp);
			if (arrMediasSelectedTmp != null && arrMediasSelectedTmp.size() > 0)
				setResult(RESULT_OK, intent);
			else
				setResult(RESULT_CANCELED);
			finish();
			arrMediasSelectedTmp.clear();
			isStartActionMode = false;
			setCheckUncheckAll(false);
			arrMediasSelected.clear();
		}

	}

	private void setTitleActionMode() {
		mActionMode.setTitle(arrMediasSelected.size() + " "
				+ getString(R.string.selected));
	}

	@Override
	public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		if (view.getId() == R.id.gridMedia) {
			if (scrollState == SCROLL_STATE_FLING) {
				mMediaAdapter.setScroolState(true);
			} else {
				mMediaAdapter.setScroolState(false);
				mMediaAdapter.notifyDataSetChanged();
			}
		}
	}

	public void setSelectedUnSelected(int position) {
		arrMedias.get(position).isChecked = !arrMedias.get(position).isChecked;
		mMediaAdapter.notifyDataSetChanged();
		if (arrMedias.get(position).isChecked)
			arrMediasSelected.add(arrMedias.get(position));
		else
			arrMediasSelected.remove(arrMedias.get(position));
		mActionMode.setTitle(arrMediasSelected.size() + " "
				+ getString(R.string.selected));
		mMediaAdapter.notifyDataSetChanged();
	}

	private void setCheckUncheckAll(boolean isChecked) {
		for (ModelMedia item : arrMedias) {
			if (isChecked) {
				item.setChecked(true);
				arrMediasSelected.add(item);
			} else {
				arrMediasSelected.clear();
				item.setChecked(false);
			}
		}
		mActionMode.setTitle(arrMediasSelected.size() + " "
				+ getString(R.string.selected));
		mMediaAdapter.notifyDataSetChanged();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		setSelectedUnSelected(position);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case Config.REQUEST_CODE_START_PATTERN_PASSWORD:
			if (resultCode == Activity.RESULT_OK) {
				IS_START_PASSWORD = false;
			}
			break;
		default:
			break;
		}

	}
}
